package org.example.src.service.impl;

import org.example.src.dto.request.JobPostRequest;
import org.example.src.dto.response.JobPostResponse;
import org.example.src.entity.Company;
import org.example.src.entity.JobPost;
import org.example.src.repository.CompanyRepository;
import org.example.src.repository.JobPostRepository;
import org.example.src.service.JobPostService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class JobPostServiceImpl implements JobPostService {

    @Autowired
    private JobPostRepository jobPostRepository;

    @Autowired
    private CompanyRepository companyRepository;

    @Override
    public List<JobPost> findAllJobPosts() {
        return jobPostRepository.findAll();
    }

    @Override
    public JobPost findJobPostById(Long id) {
        return jobPostRepository.findById(id).orElse(null);
    }

    @Override
    public JobPost createJobPost(JobPostRequest request) {
        Company company = companyRepository.findById(request.getCompanyId())
                .orElseThrow(() -> new RuntimeException("公司未找到"));

        JobPost jobPost = new JobPost();
        jobPost.setTitle(request.getTitle());
        jobPost.setDescription(request.getDescription());
        jobPost.setRequirements(request.getRequirements());
        jobPost.setLocation(request.getLocation());
        jobPost.setSalary(request.getSalary());
        jobPost.setStartDate(request.getStartDate());
        jobPost.setEndDate(request.getEndDate());
        jobPost.setIsActive(request.getIsActive() != null ? request.getIsActive() : true);
        jobPost.setCompany(company);

        return jobPostRepository.save(jobPost);
    }

    @Override
    public JobPost updateJobPost(Long id, JobPostRequest request) {
        JobPost jobPost = jobPostRepository.findById(id)
                .orElseThrow(() -> new RuntimeException("职位未找到"));

        jobPost.setTitle(request.getTitle());
        jobPost.setDescription(request.getDescription());
        jobPost.setRequirements(request.getRequirements());
        jobPost.setLocation(request.getLocation());
        jobPost.setSalary(request.getSalary());
        jobPost.setStartDate(request.getStartDate());
        jobPost.setEndDate(request.getEndDate());
        jobPost.setIsActive(request.getIsActive());

        if (request.getCompanyId() != null) {
            Company company = companyRepository.findById(request.getCompanyId())
                    .orElseThrow(() -> new RuntimeException("公司未找到"));
            jobPost.setCompany(company);
        }

        return jobPostRepository.save(jobPost);
    }

    @Override
    public void deleteJobPost(Long id) {
        jobPostRepository.deleteById(id);
    }

    @Override
    public List<JobPost> findJobPostsByCompanyId(Long companyId) {
        return jobPostRepository.findByCompanyId(companyId);
    }

    @Override
    public List<JobPost> findActiveJobPosts() {
        return jobPostRepository.findByActiveStatus(true);
    }

    @Override
    public List<JobPost> searchJobPosts(String keyword) {
        return jobPostRepository.searchByKeyword(keyword);
    }

    @Override
    public List<JobPost> findActiveJobsFromApprovedCompanies() {
        return jobPostRepository.findActiveJobsFromApprovedCompanies();
    }

    @Override
    public List<JobPostResponse> getActiveJobPosts() {
        List<JobPost> jobPosts = jobPostRepository.findActiveJobs(LocalDate.now());
        return jobPosts.stream().map(jobPost -> {
            JobPostResponse response = new JobPostResponse();
            BeanUtils.copyProperties(jobPost, response);
            if (jobPost.getCompany() != null) {
                response.setCompanyId(jobPost.getCompany().getId());
                response.setCompanyName(jobPost.getCompany().getCompanyName());
            }
            return response;
        }).collect(Collectors.toList());
    }
}
